const express=require('express')
const router= express.Router();
const db=require('../../utils/db')
const upload = require('../../utils/tupian')

module.exports = router

//查询所有菜品库存
router.get('/getMenu', function (req, res) {
    let mysql = `SELECT menu_num,menu_name,menu_price ,dish_amount,menu_src1
    FROM menu_file JOIN dish_flowing ON menu_file.menu_id=dish_flowing.dish_name ORDER BY menu_price*dish_amount DESC `
    db(mysql, null, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: '查询数据失败,请稍后重试'
            })
        }
        res.json({
            code: 200,
            msg: '查询数据成功',
            data: data
        })

    })
})
//根据菜品查询，得到数据
router.post('/getSearch', function (req, res, next) {
    let mysql = `SELECT * FROM menu_file WHERE menu_name= ?;`    
    db(mysql, req.body.menu_name, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: '查询数据失败，请稍后重试'
            })
        }
        res.json({
            code: 200,
            msg: '查询数据成功',
            data: data
        })

    })
})

//查询订单数，优惠金额，总营收，退款金额
router.get('/getOrder', function (req, res) {
    let mysql=`SELECT COUNT(*) total FROM order_information`

    db(mysql, null, function (err, data) {
        console.log(err)
        if (err) {
            return res.json({
                code: 500,
                msg: '查询数据失败,请稍后重试'
            })
        }
        res.json({
            code: 200,
            msg: '查询数据成功',
            data: data
        })

    })
})

router.get('/getMoney', function (req, res) {
    let mysql=`SELECT SUM(cash_money) money FROM cash_flowing`

    db(mysql, null, function (err, data) {
        if (err) {
            return res.json({
                code: 500,
                msg: '查询数据失败,请稍后重试'
            })
        }
        res.json({
            code: 200,
            msg: '查询数据成功',
            data: data
        })

    })
})

//在配送员认证这个页面，获取身份证示例图等数据
router.post('/insert', function (req, res, next) {
    console.log(req.body)
    let mysql = `INSERT INTO maki (maki_id,marki_name,marki_telephone,marki_type,marki_address)VALUES(null,'${req.body.name}','${req.body.tel}','${req.body.type}','${req.body.address}');`    
    db(mysql, null, function (err, data) {
        if (err) {
            console.log(err);
            return res.json({
                code: 500,
                msg: '添加数据失败，请稍后重试'
            })
        }
        res.json({
            code: 200,
            msg: '添加数据成功',
            data: data
        })

    })
})
router.post('/takeTouxiangImg', upload.single('file'), (req, res) => {
    console.log(req.file)
    res.send(req.file.filename);
})
//上传身份证照片
router.post('/gettouxiang', function (req, res, next) {
    console.log(req.body.imgName)
    let mysql = `INSERT INTO maki (marki_positive_src)VALUES('${req.body.imgName}');`    
    db(mysql, null, function (err, data) {
        if (err) {
            console.log(err);
            return res.json({
                code: 500,
                msg: '添加数据失败，请稍后重试'
            })
        }
        res.json({
            code: 200,
            msg: '添加数据成功',
            data: data
        })

    })
})

module.exports = router